using SS;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace SM.sys
{
	public class FrmRegister : Form
	{
		private XmlDocument xmlDoc = new XmlDocument();
		public bool Reg = false;
		private IContainer components = null;
		private Label label1;
		private TextBox TxtRegister;
		private Label lblTitle;
		private Label lblCDKey;
		private TextBox TxtCDKey1;
		private Button CmdExit;
		private Button CmdRegister;
		private Label label2;
		private Label label3;
		private Label label4;
		public Label lblInformation;
		private TextBox TxtCDKey2;
		private TextBox TxtCDKey3;
		private TextBox TxtCDKey4;
		private Label lblSpace1;
		private Label lblSpace2;
		private Label lblSpace3;
		public FrmRegister()
		{
			this.InitializeComponent();
		}
		private void FrmRegister_Load(object sender, EventArgs e)
		{
			try
			{
				Regis regis = new Regis();
				this.TxtRegister.Text = regis.InsertInfo("Win32_Processor");
				string cDKey = this.GetCDKey(this.TxtRegister.Text.Trim());
				if (cDKey.ToUpper() == this.GetSerial().ToUpper())
				{
					this.Reg = true;
					this.TxtRegister.Visible = false;
					this.label1.Visible = false;
					this.lblInformation.Text = "Sản phẩm đã được cấp giấy phép sử dụng";
					this.lblTitle.Text = "Bạn đã đăng ký sử dụng sản phẩm với\r\nCông ty cổ phần phần mềm Trung Việt\r\nĐể được hỗ trợ kỹ thuật, vui lòng liên hệ thông tin dưới đây";
					this.label4.Text = "";
					this.TxtCDKey1.Visible = false;
					this.TxtCDKey2.Visible = false;
					this.TxtCDKey3.Visible = false;
					this.TxtCDKey4.Visible = false;
					this.lblCDKey.Visible = false;
					this.lblSpace1.Visible = false;
					this.lblSpace2.Visible = false;
					this.lblSpace3.Visible = false;
					this.CmdRegister.Enabled = false;
				}
				else
				{
					this.Reg = false;
					this.CmdRegister.Enabled = true;
					this.lblTitle.Text = "Bạn đang sử dụng phiên bản dùng thử\r\nĐể sử dụng bản chính thức, vui lòng liên hệ với nhà cung cấp\nHãy gửi mã số đăng ký ở trên để được cung cấp một CD Key hợp lệ";
					this.TxtCDKey1.Visible = true;
					this.TxtCDKey2.Visible = true;
					this.TxtCDKey3.Visible = true;
					this.TxtCDKey4.Visible = true;
					this.lblCDKey.Visible = true;
					this.lblSpace1.Visible = true;
					this.lblSpace2.Visible = true;
					this.lblSpace3.Visible = true;
				}
			}
			catch
			{
			}
		}
		public string CryptographyMD5(string source)
		{
			string text = "";
			try
			{
				MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
				byte[] bytes = Encoding.UTF8.GetBytes(source);
				byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
				byte[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					byte b = array2[i];
					text += int.Parse(b.ToString(), NumberStyles.HexNumber).ToString();
				}
			}
			catch
			{
			}
			return text;
		}
		public string GetCDKey(string Source)
		{
			string text = "";
			string[] array = new string[]
			{
				"M",
				"I",
				"N",
				"H",
				"Q",
				"U",
				"A",
				"N",
				"G",
				"P",
				"L",
				"K",
				"J",
				"H",
				"G",
				"F",
				"D",
				"S",
				"A",
				"Z",
				"X",
				"C",
				"V",
				"B",
				"N",
				"M"
			};
			string[] array2 = new string[]
			{
				"M",
				"I",
				"N",
				"H",
				"Q",
				"U",
				"A",
				"N",
				"G",
				"P",
				"O",
				"H",
				"L",
				"K",
				"N",
				"A",
				"S",
				"D",
				"F",
				"C",
				"V",
				"Z",
				"X",
				"M",
				"J",
				"B"
			};
			if (Source != "")
			{
				text = this.CryptographyMD5(Source);
				char[] array3 = text.ToCharArray(0, text.Trim().Length);
				for (int i = 0; i < array3.Length; i++)
				{
					if (i % 2 == 1)
					{
						text += array3[i];
					}
				}
				char[] array4 = text.Trim().ToCharArray(0, text.Trim().Length);
				text = "";
				for (int j = 0; j < 20; j++)
				{
					if (j % 2 == 1)
					{
						text += array[int.Parse(array3[j].ToString())];
					}
					else
					{
						text += array2[int.Parse(array3[j].ToString())];
					}
				}
			}
			return text;
		}
		private string GetSerial()
		{
			return ConfigurationSettings.AppSettings["License"].ToString();
		}
		private void CmdRegister_Click(object sender, EventArgs e)
		{
			string text = this.TxtCDKey1.Text.Trim() + this.TxtCDKey2.Text.Trim() + this.TxtCDKey3.Text.Trim() + this.TxtCDKey4.Text.Trim();
			if (text == this.GetCDKey(this.TxtRegister.Text.Trim()))
			{
				this.UpdateKey("License", text);
				ConfigurationManager.RefreshSection("appSettings");
				this.Reg = true;
				MessageBox.Show("Bạn đã đăng ký sử dụng phần mềm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			else
			{
				this.Reg = false;
				MessageBox.Show("Mã số đăng ký không hợp lệ.\n\nKiểm tra lại hoặc liên hệ nhà cung cấp để được trợ giúp!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
		}
		private void CmdExit_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		public void UpdateKey(string strKey, string newValue)
		{
			this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "App.config");
			if (!this.KeyExists(strKey))
			{
				throw new ArgumentNullException("Key", "<" + strKey + "> does not exist in the configuration. Update failed.");
			}
			XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
			foreach (XmlNode xmlNode2 in xmlNode)
			{
				if (xmlNode2.Attributes["key"].Value == strKey)
				{
					xmlNode2.Attributes["value"].Value = newValue;
				}
			}
			this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "App.config");
			this.xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
		}
		public bool KeyExists(string strKey)
		{
			this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "App.config");
			XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
			bool result;
			foreach (XmlNode xmlNode2 in xmlNode)
			{
				if (xmlNode2.Attributes["key"].Value == strKey)
				{
					result = true;
					return result;
				}
			}
			result = false;
			return result;
		}
		private void FrmRegister_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(FrmRegister));
			this.CmdExit = new Button();
			this.CmdRegister = new Button();
			this.lblCDKey = new Label();
			this.TxtCDKey1 = new TextBox();
			this.lblTitle = new Label();
			this.lblInformation = new Label();
			this.TxtRegister = new TextBox();
			this.label1 = new Label();
			this.label2 = new Label();
			this.label3 = new Label();
			this.label4 = new Label();
			this.TxtCDKey2 = new TextBox();
			this.TxtCDKey3 = new TextBox();
			this.TxtCDKey4 = new TextBox();
			this.lblSpace1 = new Label();
			this.lblSpace2 = new Label();
			this.lblSpace3 = new Label();
			base.SuspendLayout();
			this.CmdExit.BackColor = SystemColors.ButtonFace;
			this.CmdExit.Cursor = Cursors.Hand;
			this.CmdExit.FlatAppearance.BorderColor = Color.FromArgb(0, 192, 192);
			this.CmdExit.FlatStyle = FlatStyle.System;
			this.CmdExit.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.CmdExit.ImageAlign = ContentAlignment.MiddleLeft;
//			this.CmdExit.ImeMode = ImeMode.NoControl;
			this.CmdExit.Location = new Point(317, 213);
			this.CmdExit.Name = "CmdExit";
			this.CmdExit.Size = new Size(103, 28);
			this.CmdExit.TabIndex = 25;
			this.CmdExit.Text = "Thoát  [ESC]";
			this.CmdExit.UseVisualStyleBackColor = false;
			this.CmdExit.Click += new EventHandler(this.CmdExit_Click);
			this.CmdRegister.BackColor = SystemColors.ButtonFace;
			this.CmdRegister.Cursor = Cursors.Hand;
			this.CmdRegister.FlatAppearance.BorderColor = Color.FromArgb(0, 192, 192);
			this.CmdRegister.FlatStyle = FlatStyle.System;
			this.CmdRegister.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.CmdRegister.ImageAlign = ContentAlignment.MiddleLeft;
//			this.CmdRegister.ImeMode = ImeMode.NoControl;
			this.CmdRegister.Location = new Point(230, 213);
			this.CmdRegister.Name = "CmdRegister";
			this.CmdRegister.Size = new Size(85, 28);
			this.CmdRegister.TabIndex = 24;
			this.CmdRegister.Text = "Kích hoạt";
			this.CmdRegister.UseVisualStyleBackColor = false;
			this.CmdRegister.Click += new EventHandler(this.CmdRegister_Click);
			this.lblCDKey.AutoSize = true;
			this.lblCDKey.BackColor = Color.Transparent;
			this.lblCDKey.Location = new Point(24, 128);
			this.lblCDKey.Name = "lblCDKey";
			this.lblCDKey.Size = new Size(57, 15);
			this.lblCDKey.TabIndex = 7;
			this.lblCDKey.Text = "CD Key : ";
			this.TxtCDKey1.BackColor = Color.White;
			this.TxtCDKey1.CharacterCasing = CharacterCasing.Upper;
			this.TxtCDKey1.Location = new Point(84, 125);
			this.TxtCDKey1.MaxLength = 5;
			this.TxtCDKey1.Name = "TxtCDKey1";
			this.TxtCDKey1.Size = new Size(55, 21);
			this.TxtCDKey1.TabIndex = 6;
			this.TxtCDKey1.TextAlign = HorizontalAlignment.Center;
			this.lblTitle.BackColor = Color.White;
			this.lblTitle.BorderStyle = BorderStyle.Fixed3D;
			this.lblTitle.ForeColor = Color.Red;
			this.lblTitle.Location = new Point(4, 60);
			this.lblTitle.Name = "lblTitle";
			this.lblTitle.Size = new Size(415, 58);
			this.lblTitle.TabIndex = 3;
			this.lblTitle.TextAlign = ContentAlignment.MiddleCenter;
			this.lblInformation.BackColor = Color.White;
			this.lblInformation.BorderStyle = BorderStyle.Fixed3D;
			this.lblInformation.Location = new Point(4, 6);
			this.lblInformation.Name = "lblInformation";
			this.lblInformation.Size = new Size(415, 19);
			this.lblInformation.TabIndex = 1;
			this.lblInformation.TextAlign = ContentAlignment.MiddleCenter;
			this.TxtRegister.BackColor = Color.White;
			this.TxtRegister.Location = new Point(84, 32);
			this.TxtRegister.Name = "TxtRegister";
			this.TxtRegister.ReadOnly = true;
			this.TxtRegister.Size = new Size(335, 21);
			this.TxtRegister.TabIndex = 2;
			this.TxtRegister.TextAlign = HorizontalAlignment.Center;
			this.label1.BackColor = Color.Transparent;
			this.label1.Location = new Point(7, 35);
			this.label1.Name = "label1";
			this.label1.Size = new Size(75, 15);
			this.label1.TabIndex = 1;
			this.label1.Text = "Mã đăng ký : ";
			this.label2.AutoSize = true;
			this.label2.BackColor = Color.Transparent;
			this.label2.Location = new Point(6, 179);
			this.label2.Name = "label2";
			this.label2.Size = new Size(315, 15);
			this.label2.TabIndex = 26;
			this.label2.Text = "ĐT hỗ trợ : 0388.903.452 - 0383.552.896 - 0904.686.357";
			this.label3.AutoSize = true;
			this.label3.BackColor = Color.Transparent;
			this.label3.Location = new Point(6, 199);
			this.label3.Name = "label3";
			this.label3.Size = new Size(200, 15);
			this.label3.TabIndex = 27;
			this.label3.Text = "Email : info@trungvietsoftware.com";
			this.label4.AutoSize = true;
			this.label4.BackColor = Color.Transparent;
			this.label4.Font = new Font("Arial", 9f, FontStyle.Italic, GraphicsUnit.Point, 163);
			this.label4.Location = new Point(156, 155);
			this.label4.Name = "label4";
			this.label4.Size = new Size(265, 15);
			this.label4.TabIndex = 28;
			this.label4.Text = "[Nhập vào số CD Key hợp lệ đã được cung cấp]";
			this.TxtCDKey2.BackColor = Color.White;
			this.TxtCDKey2.CharacterCasing = CharacterCasing.Upper;
			this.TxtCDKey2.Location = new Point(154, 125);
			this.TxtCDKey2.MaxLength = 5;
			this.TxtCDKey2.Name = "TxtCDKey2";
			this.TxtCDKey2.Size = new Size(55, 21);
			this.TxtCDKey2.TabIndex = 29;
			this.TxtCDKey2.TextAlign = HorizontalAlignment.Center;
			this.TxtCDKey3.BackColor = Color.White;
			this.TxtCDKey3.CharacterCasing = CharacterCasing.Upper;
			this.TxtCDKey3.Location = new Point(224, 125);
			this.TxtCDKey3.MaxLength = 5;
			this.TxtCDKey3.Name = "TxtCDKey3";
			this.TxtCDKey3.Size = new Size(55, 21);
			this.TxtCDKey3.TabIndex = 30;
			this.TxtCDKey3.TextAlign = HorizontalAlignment.Center;
			this.TxtCDKey4.BackColor = Color.White;
			this.TxtCDKey4.CharacterCasing = CharacterCasing.Upper;
			this.TxtCDKey4.Location = new Point(294, 125);
			this.TxtCDKey4.MaxLength = 5;
			this.TxtCDKey4.Name = "TxtCDKey4";
			this.TxtCDKey4.Size = new Size(55, 21);
			this.TxtCDKey4.TabIndex = 31;
			this.TxtCDKey4.TextAlign = HorizontalAlignment.Center;
			this.lblSpace1.AutoSize = true;
			this.lblSpace1.BackColor = Color.Transparent;
			this.lblSpace1.Location = new Point(140, 128);
			this.lblSpace1.Name = "lblSpace1";
			this.lblSpace1.Size = new Size(11, 15);
			this.lblSpace1.TabIndex = 33;
			this.lblSpace1.Text = "-";
			this.lblSpace2.AutoSize = true;
			this.lblSpace2.BackColor = Color.Transparent;
			this.lblSpace2.Location = new Point(211, 128);
			this.lblSpace2.Name = "lblSpace2";
			this.lblSpace2.Size = new Size(11, 15);
			this.lblSpace2.TabIndex = 34;
			this.lblSpace2.Text = "-";
			this.lblSpace3.AutoSize = true;
			this.lblSpace3.BackColor = Color.Transparent;
			this.lblSpace3.Location = new Point(281, 128);
			this.lblSpace3.Name = "lblSpace3";
			this.lblSpace3.Size = new Size(11, 15);
			this.lblSpace3.TabIndex = 35;
			this.lblSpace3.Text = "-";
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(424, 245);
			base.Controls.Add(this.lblSpace3);
			base.Controls.Add(this.lblSpace2);
			base.Controls.Add(this.lblSpace1);
			base.Controls.Add(this.TxtCDKey4);
			base.Controls.Add(this.TxtCDKey3);
			base.Controls.Add(this.TxtCDKey2);
			base.Controls.Add(this.label4);
			base.Controls.Add(this.label3);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.lblInformation);
			base.Controls.Add(this.CmdExit);
			base.Controls.Add(this.CmdRegister);
			base.Controls.Add(this.lblTitle);
			base.Controls.Add(this.lblCDKey);
			base.Controls.Add(this.TxtCDKey1);
			base.Controls.Add(this.TxtRegister);
			base.Controls.Add(this.label1);
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.Margin = new Padding(3, 4, 3, 4);
			base.MaximizeBox = false;
			this.MaximumSize = new Size(440, 279);
			base.MinimizeBox = false;
			this.MinimumSize = new Size(440, 279);
			base.Name = "FrmRegister";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = " .: Bản quyền sử dụng";
			base.Load += new EventHandler(this.FrmRegister_Load);
			base.KeyDown += new KeyEventHandler(this.FrmRegister_KeyDown);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
